package com.apes.fn.mdm.offer.service;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.fn.mdm.offer.model.DeptOfferGroup;
import com.apes.fn.mdm.offer.model.OfferGroup;
import com.apes.fn.mdm.offer.repository.DeptOfferGroupRepository;
import com.apes.fn.mdm.offer.repository.OfferGroupRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.jpa.spec.JpaDsl;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.scm.masterdata.frame.model.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class DeptOfferGroupService extends DomainService {


    @Autowired
    private DeptOfferGroupRepository deptOfferGroupRepository;

    @Autowired
    private OfferGroupRepository offerGroupRepository;


    /**
     * 门店报价组新增
     * @param request
     * @return
     */
    public DeptOfferGroup save(SimpleRequest request){
        JSONObject jo = request.getJO();
        Dept dept = jo.getObject("dept", Dept.class);
        DeptOfferGroup deptOfferGroup = new DeptOfferGroup();
        deptOfferGroup.setName(jo.getJSONObject("offerGroup").getString("name"));
        deptOfferGroup.setDept(dept);
        try{
            return  deptOfferGroupRepository.saveAndFlush(deptOfferGroup);
        }catch (RuntimeException e){
            throw new RuntimeException("该门店已添加报价组!");
        }

    }

    /**
     * 批量修改门店
     * @param request
     * @return
     */
    public JSONObject edit(SimpleRequest request){
        JSONObject jo = request.getJO();
        JSONArray deptOfferGroupJSON = jo.getJSONArray("deptOfferGroupItem");
        List<DeptOfferGroup> deptOfferGroupList = JSONArray.parseArray(deptOfferGroupJSON.toJSONString(),DeptOfferGroup.class);
        deptOfferGroupList.stream().forEach(item->{
            item.setName(item.getName().toUpperCase());
         List<String> nameList = offerGroupRepository.findAllName();
         if(!nameList.contains(item.getName())) throw new RuntimeException(item.getName()+"未找到该报价组！");
            deptOfferGroupRepository.saveAndFlush(item);
        });
        Stream<String> stream = deptOfferGroupList.stream().map(DeptOfferGroup :: getName).distinct();
        List<String> nameList = stream.collect(Collectors.toList());
        List<OfferGroup> offerGroups = offerGroupRepository.findByNameIn(nameList);
        jo.put("deptOfferGroupItem",deptOfferGroupList);
        jo.put("offerGroupItem",offerGroups);
        return  jo;
    }


    /**
     * 采购组配置界面查找数据
     * @param request
     * @return
     */
    public JSONObject getDeptOfferGroupList(SimpleRequest request) {
        JSONObject getJO = request.getJO();
        OfferGroup offerGroup = getJO.getObject("offerGroup",OfferGroup.class);
        String name = null;
        if(offerGroup!=null){
            name = offerGroup.getName();
        }
        Dept dept = getJO.getObject("dept",Dept.class);
            JSONObject returnData = new JSONObject();
            List<DeptOfferGroup> deptOfferGroups = deptOfferGroupRepository.findAll(JpaDsl.toCriteriaIgnoreNull("dept","EQ",dept,"name","EQ",name));
            if (deptOfferGroups.size() == 0) throw new RuntimeException("未找到相关数据");
            Stream<String> stream = deptOfferGroups.stream().map(DeptOfferGroup :: getName).distinct();
            List<String> nameList = stream.collect(Collectors.toList());
            List<OfferGroup> offerGroups = offerGroupRepository.findByNameIn(nameList);
            returnData.put("offerGroupItem", offerGroups);
            returnData.put("deptOfferGroupItem", deptOfferGroups);
        return returnData;
    }

    /**
     *
     * @param request
     * @return
     */
    public boolean delete(SimpleRequest request){
        JSONObject jo = request.getJO();
        DeptOfferGroup deptOfferGroup = deptOfferGroupRepository.findByDeptAndName(jo.getJSONObject("dept").getString("id"),jo.getJSONObject("offerGroup").getString("name"));
        if(deptOfferGroup==null)return false;
        deptOfferGroupRepository.delete(deptOfferGroup);
        return true;
    }


    /**
     * 根据报价组名称查询门店
     * @param request
     * @return
     */
    public List<String> fiindAllName(SimpleRequest request){
        JSONObject jo = request.getJO();
        return  deptOfferGroupRepository.findByName(jo.getString("name"));

    }

    /**
     * 根据门店查询报价组
     * @param request
     * @return
     */
    public DeptOfferGroup findByDeptId(SimpleRequest request){
        JSONObject jo = request.getJO();
        return  deptOfferGroupRepository.findByDeptId(jo.getString("deptId"));

    }

    /**
     * 根据报价组ID查询门店
     * @param request
     * @return
     */
    public List<String> fiindByOfferGroup(SimpleRequest request){
        JSONObject jo = request.getJO();
        OfferGroup offerGroup =   offerGroupRepository.findOne(jo.getString("offerGroup"));
        return  deptOfferGroupRepository.findByName(offerGroup.getName());

    }

    /**
     * 根据门店id获取报价组联系人信息
     * @param request
     * @return
     */
    public Map queryContactPersonInfoByDeptId(SimpleRequest request){
        JSONObject jo = request.getJO();
        Map map = deptOfferGroupRepository.queryContactPersonInfoByDeptId(jo.getString("deptId"));
        return map;
    }
}
